#pragma once
#include <iostream>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <cassert>
#include "log.hpp"

using namespace std;

#define PATH_NAME "/home/zy/linux-test/2.pipe/shmem"
#define PROJ_ID 0x66
#define SHM_SIZE 4096

//////////用管道实现共享内存的同步(达到访问控制的效果)//////////
#define FIFO_NAME "./fifo"

class Init
{
public:
    // 构造的时候创建管道
    Init()
    {
        umask(0);
        int n = mkfifo(FIFO_NAME, 0666);
        assert(n == 0);
        (void)n;
        Log("create fifo success", Notice) << endl;
    }
    // 删除管道文件
    ~Init()
    {
        unlink(FIFO_NAME);
        Log("remove fifo success", Notice) << endl;
    }
};


#define READ O_RDONLY
#define WRITE O_WRONLY

int OpenFIFO(const string PathName, int flags)
{
    int fd = open(PathName.c_str(), flags);
    assert(fd >= 0);
    return fd;
}


void Wait(int fd)
{
    Log("等待中...",Notice)<<endl;
    u_int32_t tmp = 0;
    ssize_t s = read(fd,&tmp,sizeof(uint32_t));
    assert(s == sizeof(u_int32_t));
    (void)s;
}

void Signal(int fd)
{
    Log("唤醒中...",Notice)<<endl;
    u_int32_t tmp = 1;
    ssize_t s = write(fd,&tmp,sizeof(uint32_t));
    assert(s == sizeof(u_int32_t));
    (void)s;
}


void CloseFifo(int fd)
{
    close(fd);
}
